//按键操作
#include "APP_Main.h"
#include "Keyboard.h"

_key_param key_param;

uint16_t minuteCount = 0;
uint16_t key_long = 0;
extern uint8_t Factory_Enable;
extern uint8_t Factory_LCD_ONOFF;


//通过示波器测量按键延迟, 15ms: 计数: 2500 * 16
void keyboard_delay15ms(void)
{
    // for (uint32_t i = 0; i < 2500 * 16; i++)
    // {
    // }
    // delay_nms(15);
    delay_nms(12);
}

//Key1,Key2
void KeyDetection(void)
{
    static uint8_t lock = 0;

    //key1, 单位切换
    if(ReadIO_F_C == 0)
    {
        keyboard_delay15ms();
        if ((ReadIO_F_C == 0) && (key_param.once2 == 0)) key_param.once2 = 1; //按键有效,但不处理
    }
    else if (ReadIO_F_C > 0)
    {
        keyboard_delay15ms();
        if(key_param.once2 > 0)
        { //按键释放后,处理上次有效按键
            key_param.once2 = 0;

            if(Factory_Enable > 0)
            { //出厂测试模式
                if(Fan_State.Heater == 0) 
				{
					IgniterOpen;
				}
                else IgniterClose;
                // Buzzer_Once();
            }
            else
            {
                if(UI_System.Grill_Unit == Unit_C) UI_System.Grill_Unit = Unit_F;
                else UI_System.Grill_Unit = Unit_C; 
                Flash_Write();
            }
        }
    }
    //key2, 电源按键
    if(ReadIO_ON_OFF == 0)
    {
        keyboard_delay15ms();
        if (ReadIO_ON_OFF == 0)
        {
            if(key_long == 0) key_long = 1;
            else if(key_long >= 3)
            { //长按3s
                if(lock == 0)
                {
                    lock = 1;

                    if(Factory_Enable == 0)
                    {
                        UI_TempCtrl_Stop();
                        Buzzer_Once();
                    }
                }
                key_param.once3 = 0; 
            }
            else if(key_param.once3 == 0) key_param.once3 = 1; //按键有效,但不处理
        }
    }
    else if (ReadIO_ON_OFF > 0)
    {
        keyboard_delay15ms();
        if(key_param.once3 > 0)
        { //按键释放后,处理上次有效按键
            key_param.once3 = 0;

            if(Factory_Enable > 0)
            { //出厂测试模式
                Buzzer_Once();

                if(Factory_LCD_ONOFF == 0)
                {
                    Factory_LCD_ONOFF = 1;
                    GN1621_ON();
                    GN1621_RAM_Reset(0xff); 

                    ExhaustFanOpen;
                }
                else
                {
                    Factory_LCD_ONOFF = 0; 
                    Buzzer_Alarm();
                    GN1621_OFF();

                    ExhaustFanClose;
                }
            }
            else
            { 
                if(UI_System.Grill_state == State_Grill_Idle) UI_System.Grill_state = State_Grill_SetTemp;
                else if(UI_System.Grill_state == State_Grill_Misfire)
                {
                    GN1621_ON();
                    Lcd_Parts_Init();
                    UI_System.Grill_state = State_Grill_SetTemp;
                    Buzzer_Once();
                }
            }
        }
        lock = 0; 
        key_long = 0;
    }
}

void Key_IOConfig(void)
{
    //Test code
	GPIO_Init(GPIOA0,Key_On_Off,Intput);
    GPIO_Init(GPIOA0,Key_F_C,Intput);
	GPIO_Init(GPIOA0,Encode_KEY,Intput);
	GPIO_PullHigh_Init(GPIOA0,Encode_KEY); 
//	GPIO_PullHigh_Init(GPIOA0,Key_On_Off); 
//	GPIO_PullHigh_Init(GPIOA0,Key_F_C); 
 
	key_param.once2 = 0;
}
void Key_Init(void)
{
    Key_IOConfig();
    key_param.once2 = 0;
}
void Key_Main(void)
{
    KeyDetection();
    if(minuteCount >= 500)
    { //Tick 1s = 2ms * 500
        minuteCount-= 500;
    }
}
//Tick 10s
void Key_Clock(void)
{
    if((key_long > 0) && (key_long < 0xffff)) key_long++;
}
